package com.songx64.eduservice.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.songx64.commonutils.R;
import com.songx64.eduservice.entity.EduTeacher;
import com.songx64.eduservice.entity.vo.EduTeacherQuery;
import com.songx64.eduservice.service.EduTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 讲师 前端控制器
 *
 * @author songx64
 * @since 2020-10-16
 */

@Api(tags = "讲师管理控制器")
@RestController
@RequestMapping("/eduservice/edu-teacher")
public class EduTeacherController {
    @Autowired
    private EduTeacherService eduTeacherService;

    /**
     * 1.List方法，查询所有讲师
     */
    @ApiOperation(value = "列出所有讲师")
    @GetMapping("/findAll")
    public R findAllTeacher() {
        List<EduTeacher> list = eduTeacherService.list(null);
        return R.ok().data("allTeachers", list);
    }

    /**
     * 2.根据id删除
     */
    @ApiOperation(value = "删除讲师", notes = "根据Id删除讲师")
    @DeleteMapping("/{id}")
    public R deleteTeacherById(
            @ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable String id) {
        //TODO 删除只是表示数据库语句是否执行成功，Update 0返回的也是true
        return eduTeacherService.removeById(id) ? R.ok() : R.error();
    }

    /**
     * 3.分页查询方法
     * current 当前页
     * limit 每页记录数
     */
    @ApiOperation("分页查询讲师")
    @GetMapping("pageTeacher/{current}/{limit}")
    public R pageListTeacher(
            @ApiParam(name = "current", value = "当前页数")
            @PathVariable Long current,
            @ApiParam(name = "limit", value = "每页记录数")
            @PathVariable Long limit) {
        //创建Page对象，传入参数当前页1和每页记录数3
        Page<EduTeacher> pageTeacher = new Page<>(current, limit);
        //调用方法实现分页:分页对象，条件
        //封装自带的page方法，直接分页查询，结果放入Page对象中
        eduTeacherService.page(pageTeacher, null);
        //从Page对象中得到查询结果,total是总条数
        List<EduTeacher> records = pageTeacher.getRecords();
        Long total = pageTeacher.getTotal();
        return R.ok().data("total", total).data("rows", records);
    }


    /*
     * 4. 条件分页查询方法
     * */
    @ApiOperation("条件分页查询")
    @PostMapping("pageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondition(@ApiParam(name = "current", value = "当前页数")
                                  @PathVariable Long current,
                                  @ApiParam(name = "limit", value = "每页记录数")
                                  @PathVariable Long limit,
                                  @RequestBody(required = false) EduTeacherQuery eduTeacherQuery) {
        //新建Page对象
        Page<EduTeacher> pageTeahcer = new Page<>(current, limit);
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        //多条件查询，可以通过xml动态sql
        //这里单表可以直接通过判断拼接，条件值不为空就拼接条件
        String name = eduTeacherQuery.getName();
        Integer level = eduTeacherQuery.getLevel();
        String begin = eduTeacherQuery.getBegin();
        String end = eduTeacherQuery.getEnd();
        //StringUtils是springframework包中的字符串工具类
        if (!StringUtils.isEmpty(name)) {
            //构造条件
            wrapper.like("name", name);
        }
        if (!StringUtils.isEmpty(level)) {
            wrapper.eq("level", level);
        }
        if (!StringUtils.isEmpty(begin)) {
            wrapper.ge("gmt_create", begin);
        }
        if (!StringUtils.isEmpty(end)) {
            wrapper.le("gmt_create", end);
        }
        //条件查询
        eduTeacherService.page(pageTeahcer, wrapper);

        Long total = pageTeahcer.getTotal();
        List<EduTeacher> records = pageTeahcer.getRecords();
        return R.ok().data("total", total).data("rows", records);
    }


    /*
     * 5.添加讲师方法
     * */
    @ApiOperation("添加讲师")
    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody EduTeacher eduTeacher) {
        boolean save = eduTeacherService.save(eduTeacher);
        if (save) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    /*
     * 6.根据讲师id进行查询
     * */
    @ApiOperation("根据Id查询讲师")
    @GetMapping("getTeacher/{id}")
    public R getTeacher(@PathVariable String id) {
        EduTeacher eduTeacher = eduTeacherService.getById(id);
        return R.ok().data("teacher", eduTeacher);
    }

    /*
     * 7.1 讲师修改功能
     * */
    @ApiOperation("根据ID修改讲师，Post方法")
    @PostMapping("updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher eduTeacher) {
        boolean flag = eduTeacherService.updateById(eduTeacher);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    /*
     * 7.2 Put方式修改讲师
     * */
    @ApiOperation("根据ID修改讲师，Put方法")
    @PutMapping("{id}")
    public R updateTeacherById(@PathVariable String id, @RequestBody EduTeacher eduTeacher) {
        eduTeacher.setId(id);
        boolean flag = eduTeacherService.updateById(eduTeacher);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }

    }

}


